package com.teame.readbaby.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterForLinearLayout extends BaseAdapter {// 二话不说，继承先
	
	private LayoutInflater mInflater;
	
	private int resource;
	
	private List<? extends Map<String, ?>> data;
	
	private String[] from;
	
	private int[] to;
	
	// 这个我是仿照simpleAdapter搞的
	
	public AdapterForLinearLayout(Context context,
			List<? extends Map<String, ?>> data, int resource,
					String[] from, int[] to) {
		
		this.data = data;
		
		this.resource = resource;
		
		this.from = from;
		
		this.to = to;
		
		this.mInflater = LayoutInflater.from(context);
		
	}
	
	@Override
	public int getCount() {
		
		return data.size();
		
	}
	
	@Override
	public Object getItem(int position) {
		
		return data.get(position);
		
	}
	
	@Override
	public long getItemId(int position) {
		
		return position;
		
	}
	
	// 亮点在这里，getView方法呈现
	
	@SuppressWarnings("unchecked")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		convertView = mInflater.inflate(this.resource, null);
		
		Map item = data.get(position);
		
		int count = to.length;
		
		for (int i = 0; i < count; i++) {
			
			View v = convertView.findViewById(to[i]);
			
			bindView(v, item, from[i]);
			
		}
		
		convertView.setTag(position);// 一定要注意这个东西，这个可是我们判断哪一个子项被点击的重要依据，你可以往里面set任何对象的
		
		return convertView;
		
	}
	
	// 接下来是bindview，我在这里面实现了imageview的功能，simpleAdapter没有的东西哟
	
	@SuppressWarnings("unchecked")
	private void bindView(View v, Map item, String from) {
		
		Object data = item.get(from);
		
		if (v instanceof Checkable) {
			
			if (data instanceof Boolean) {
				
				((Checkable) v).setChecked((Boolean) data);
				
			} else {
				
				throw new IllegalStateException(v.getClass().getName()
						
						+ " should be bound to a Boolean, not a "
						
					+ data.getClass());
				
			}
			
		} else if (v instanceof TextView) {
			
			((TextView) v).setText(data == null ? "" : data.toString());
			
		} else if (v instanceof ImageView) {
			
			if (data != null && !data.equals("")) {
				
				if (data instanceof Integer) {
					
					((ImageView) v).setImageResource((Integer) data);
					
				} else {
					
					//TODO 日后研究
//						Bitmap bm = ImageCache.getInstance().get(
//								// 这方法是自己写的
//								data.toString(),(ImageView) v);
//
//						((ImageView) v).setImageBitmap(bm);
//
//						((ImageView) v).setPadding(2, 2, 2, 2);
					
				}
				
			}
			
		} else {
			
			throw new IllegalStateException(v.getClass().getName()
					
					+ " is not a "
					
				+ " view that can be bounds by this SimpleAdapter");
			
		}
		
	}
}